package com.example.service;

import com.example.mapper.DrugCompanyMapper;
import com.example.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DrugCompanyService {

    @Autowired
    private DrugCompanyMapper drugCompanyMapper;

    public List<DrugCompany> getCompanyList(ConditionsQuery conditionsQuery) {

        return drugCompanyMapper.getCompanyList(conditionsQuery);
    }


    public Result addDrugCompany(DrugCompany drugCompany) {
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(drugCompany.getCompany_name())){
            return new Result(100,"公司名不能为空",null);
        }

        if (!StringUtils.hasText(drugCompany.getCompany_phone())){
            return new Result(100,"电话不能为空",null);
        }

        drugCompany.setCreatetime(new Date());
        drugCompany.setUpdatetime(new Date());
        int row = drugCompanyMapper.insert(drugCompany);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"修改成功",null);

    }

    public Result editDrugCompany(DrugCompany drugCompany) {
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(drugCompany.getCompany_name())){
            return new Result(100,"公司名不能为空",null);
        }

        if (!StringUtils.hasText(drugCompany.getCompany_phone())){
            return new Result(100,"电话不能为空",null);
        }

        drugCompany.setUpdatetime(new Date());
        int row = drugCompanyMapper.update(drugCompany);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"修改成功",null);
    }

    public Result delById(int id) {
        /**
         * 查询是否存在
         */
        DrugCompany d=drugCompanyMapper.getDrugCompanyById(id);
        //System.err.println(d);
        if(d==null){
            return new Result(100,"公司不存在",null);
        }
        /**
         * 先删除用户表账号
         */
        int row =drugCompanyMapper.delById(id);
        //System.err.println(row);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }

    public Result delBatch(String ids) {
        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =drugCompanyMapper.delBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }

    public List<CompanyPolicy> getCompanyPolicyList(ConditionsQuery conditionsQuery) {
        return drugCompanyMapper.getCompanyPolicyList(conditionsQuery);
    }

    public Result addCompanyPolicy(CompanyPolicy companyPolicy) {
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(companyPolicy.getCompany_name())){
            return new Result(100,"公司名不能为空",null);
        }

        if (!StringUtils.hasText(companyPolicy.getTitle())){
            return new Result(100,"标题不能为空",null);
        }
        if (!StringUtils.hasText(companyPolicy.getMessage())){
            return new Result(100,"内容不能为空",null);
        }

        ConditionsQuery conditionsQuery = new ConditionsQuery();
        conditionsQuery.setPageNum(1);
        conditionsQuery.setPageSize(10);
        conditionsQuery.addCondition("company_name",companyPolicy.getCompany_name());
        DrugCompany drugCompany=drugCompanyMapper.getCompanyList(conditionsQuery).get(0);
        if(drugCompany==null){
            return new Result(100,"公司名称不存在",null);
        }
        //System.err.println(drugCompany);
        companyPolicy.setCompany_id(drugCompany.getCompany_id());
        companyPolicy.setCreate_time(new Date());
        companyPolicy.setUpdate_time(new Date());
        int row = drugCompanyMapper.insertCompanyPolicy(companyPolicy);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"修改成功",null);
    }

    public Result delCompanyPolicyById(int id) {
        /**
         * 查询是否存在
         */
        CompanyPolicy d=drugCompanyMapper.getCompanyPolicyById(id);
        if(d==null){
            return new Result(100,"政策不存在",null);
        }

        int row =drugCompanyMapper.delCompanyPolicyById(id);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }

    public Result delCompanyPolicyBatch(String ids) {
        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =drugCompanyMapper.delCompanyPolicyBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }

    /**
     * 需要传入id
     * @param companyPolicy
     * @return
     */
    public Result editCompanyPolicy(CompanyPolicy companyPolicy) {
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(companyPolicy.getTitle())){
            return new Result(100,"标题不能为空",null);
        }

        if (!StringUtils.hasText(companyPolicy.getMessage())){
            return new Result(100,"内容不能为空",null);
        }

        companyPolicy.setUpdate_time(new Date());

        int row = drugCompanyMapper.updateCompanyPolicy(companyPolicy);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"修改成功",null);
    }

    public Result getList() {
        List<DrugCompany> drugCompanies=drugCompanyMapper.getList();
        return new Result(200,"数据加载成功",drugCompanies);
    }
}
